import { app } from "../apiRoot";
import { defHttp } from '@/utils/http';
import { ChatLaunchRequest, ChatLaunchResponse, ChatRetrieveRequest, ChatRetrieveResponse, CreateChatResponse, MessageRecord, ConversationMessageListRequest, BotInfo, AddFriendInfo } from "./types";
import { StreamBuilder, createStreamBuilder } from '@/utils/http/streamRequest';
import { IResData } from "../types";


const Api = {
    conversationCreate: 'ai/ai-user-bot_chat_record/conversationCreate',
    chatLaunch: 'ai/ai-agent/chatLaunch',
    chatRetrieve: 'ai/ai-agent/chatRetrieve',
    chatMessageList: 'ai/ai-agent/chatMessageList',
    chatCancel: 'ai/ai-agent/chatCancel',
    conversationMessageList: 'ai/ai-user-bot_chat_record/conversationMessageList',
    getStreamMessage: 'ai/ai-user-bot_chat_record/addQuestion',
    addAnswer: 'ai/ai-user-bot_chat_record/addAnswer',
    getBotById: 'ai/app_bot/getBotById',
    addFriend: 'ai/bot/friend/apply',
}


/**
 * 添加好友
 * @param data 添加好友信息
 * @returns 添加好友信息
 */
export function addFriend(data: AddFriendInfo) {
    return defHttp.post({
        url: Api.addFriend,
        data,
    });
}

/**
 * 获取智能体信息
 * @param uuid 智能体ID
 * @returns 智能体信息
 */
export function getBotById(uuid: string) {
    return defHttp.get<BotInfo>({
        url: Api.getBotById + '/' + uuid,
    });
}


/**
 * 创建智能体
 * @param botId 
 * @returns 
 */
export function conversationCreate(uuid: string) {
    return defHttp.post<CreateChatResponse>({
        url: Api.conversationCreate,
        data: { uuid },
    });
}


/**
 * 发起会话
 * @param data 
 * @returns 
 */
export function chatLaunch(data: ChatLaunchRequest) {
    return defHttp.post<ChatLaunchResponse>({
        url: Api.chatLaunch,
        data,
    });
}


/**
 * 查看对话详情
 * @param data 
 * @returns 
 */
export function chatRetrieve(data: ChatRetrieveRequest) {
    return defHttp.post<ChatRetrieveResponse>({
        url: Api.chatRetrieve,
        data,
    });
}

/**
 * 查询消息返回内容
 * @param data 
 * @returns 
 */
export function chatMessageList(data: ChatRetrieveRequest) {
    return defHttp.post<any>({
        url: Api.chatMessageList,
        data,
    });
}

/**
 * 保存回答历史记录
 * @param data 
 * @returns 
 */
export function addAnswer(data: ChatLaunchRequest) {
    return defHttp.post<any>({
        url: Api.addAnswer,
        data,
    });
}


/**
 * 取消进行中的对话
 * @param data 
 * @returns 
 */
export function chatCancel(data: ChatRetrieveRequest) {
    return defHttp.post({
        url: Api.chatCancel,
        data,
    });
}

/**
 * 查询聊天记录（支持分页）
 * @param params 查询参数
 * @returns 
 */
export function conversationMessageList(params: ConversationMessageListRequest) {
    return defHttp.post<IResData<MessageRecord>>({
        url: Api.conversationMessageList + '?current=' + params.current + '&size=' + params.size,
        data: params,
    });
}

/**
 * 流式聊天请求
 * @param data 请求数据
 * @returns 流构建器，支持链式调用
 */
export function streamChat(data: ChatLaunchRequest): StreamBuilder {
    return createStreamBuilder({
        url: Api.getStreamMessage,
        method: 'POST',
        data,
    });
}

